import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { Log4jsLogger } from '@nestx-log4js/core';
import { AppModule } from './app.module';

const lsitenPort = 3000;
const logger = new Logger('main.ts');

/**
 * 主方法
 * @date 2021-08-09
 * @returns {any}
 */
const bootstrap = async () => {
  /**
   * 配置Swagger
   */
  const app = await NestFactory.create(AppModule);
  const config = new DocumentBuilder()
    .setTitle('CodeLearn平台')
    .setDescription('CodeLearn平台 API description')
    .setVersion('1.0')
    .addBearerAuth(
      {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
      },
      'jwt',
    )
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('swagger-ui', app, document);
  /**
   * 配置Log4js
   */
  app.useLogger(app.get(Log4jsLogger));
  await app.listen(lsitenPort);
};

bootstrap().then(() => {
  logger.log(`-----------------------------------------`);
  logger.log(`CodeLearn-backEnd-1.0.0`);
  logger.log(`listen in http://localhost:${lsitenPort}/swagger-ui`);
  logger.log(`-----------------------------------------`);
});
